package list;

import java.util.Iterator;

/**
 * @Auther: liwenhao
 * @Date: 2022/5/12 15:37
 * @Description:
 */
public class TowWayLinkList<T> implements Iterable<T> {

    //头结点
    private Node<T> head;

    //尾结点
    private Node<T> last;

    //节点数
    private volatile int N;

    public TowWayLinkList() {
        N = 0;
        head = new Node<>(null, null, null);
        last = null;
    }

    //空置链表
    public void clear(){
        head.next = null;
        head.item = null;
        head.pre = null;
        last = null;
        N  = 0;
    }

    //判断是否为空
    public boolean isEmpty(){
        return N == 0;
    }

    //链表长度
    public int length(){
        return N;
    }

    // 返回第I个元素
    public T get(int i){
        if (i > N || i < 0) {
            throw new RuntimeException("位置不合法！");
        }
        Node<T> node = head.next;
        for (int j = 0; j < i; j++) {
            node = node.next;
        }
        return  node.item;
    }

    //链表中添加元素
    public void insert(T t){
        if (null == last) {
            last = new Node(t, head, null);
            head.next = last;
        }else {
            Node<T> oldNode = this.last;
            Node<T> node = new Node<>(t, oldNode, null);
            last = node;
            oldNode.next = node;
        }
        N++;
    }

    //指定位置插入
    public void insert(int i, T t){
        if (i > N || i < 0) {
            throw new RuntimeException("位置不合法！");
        }
        //前一个节点
        Node<T> pre = head.next;
        for (int j = 0; j < i; j++) {
            pre = pre.next;
        }
        //当前节点
        Node<T> cur = pre.next;
        Node<T> tNode = new Node<>(t, pre, cur);
        pre.next = tNode;
        cur.pre = tNode;
        N++;

    }

    //删除指定位置元素
    public T remove(int i){
        if (i > N || i < 0) {
            throw new RuntimeException("位置不合法！");
        }
        //前一个节点
        Node<T> cur = head.next;
        for (int j = 0; j <= i; j++) {
            cur = cur.next;
        }
        Node pre = cur.pre;
        Node next = cur.next;
        pre.next = next;
        next.pre = pre;
        N--;
        return cur.item;
    }

    //元素位置
    public int indexOf(T t){
        Node n = head;
        for (int i = 0;n.next!=null;i++){
            n = n.next;
            if (n.item.equals(t)) {
                return i;
            }
        }
        return -1;
    }

    //获取第一个元素
    public T getFirst(){
        if (isEmpty()) {
            return null;
        }
        return (T) head.next.item;
    }

    //获取最后一个元素
    public T getLast(){
        if (isEmpty()) {
            return null;
        }
        return last.item;
    }



    @Override
    public Iterator<T> iterator() {
        return new TIterator();
    }


    private class TIterator implements Iterator<T>{
        private Node<T> n;

        public TIterator() {
            this.n = head;
        }

        @Override
        public boolean hasNext() {
            return n.next != null;
        }

        @Override
        public T next() {
            n = n.next;
            return n.item;
        }
    }

    class Node<T>{
        //数据
        T item;
        //前一个节点
        Node pre;
        //后一个节点
        Node next;

        public Node(T item, Node pre, Node next) {
            this.item = item;
            this.pre = pre;
            this.next = next;
        }
    }
}
